package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.organization.RCCEORoleProvider;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCOrganizationService;
import com.shrct.businessprocess.organization.RCSalesLeaderRoleProvider;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.project.RCInitializeProjectProcess;
import com.shrct.businessprocess.project.RCParameterService;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectProcessEvent;
import com.shrct.businessprocess.project.RCProjectProcessEventStatus;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.project.RCProjectStatus;
import com.shrct.businessprocess.project.events.RCProjectEventsManager;
import com.shrct.businessprocess.project.impl.RCProjectImpl;
import com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl;
import com.shrct.businessprocess.project.proxy.RCInitializeProjectProcessProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.task.NormalTask;
import edu.fudan.langlab.domain.task.TaskService;
import edu.fudan.langlab.domain.workflow.ActivityNodeDef;
import edu.fudan.langlab.domain.workflow.BusinessProcess;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.langlab.domain.workflow.impl.BusinessProcessImpl;
import edu.fudan.mylang.common.StringExtensions;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCINITIALIZEPROJECTPROCESS_TYPE")
public class RCInitializeProjectProcessImpl extends BusinessProcessImpl implements RCInitializeProjectProcess {
  public RCInitializeProjectProcessImpl() {
    super();
  }
  
  public String getProcessName() {
    return "项目审批流程";
  }
  
  public String getProcessType() {
    return "RCInitializeProjectProcess";
  }
  
  public ActivityNodeDef getActivityDef(final String activityId) {
    edu.fudan.langlab.domain.workflow.ActivityNodeDef result = new edu.fudan.langlab.domain.workflow.ActivityNodeDef();
    result.setActivityId(activityId);
    if("__end__".equals(activityId)){
    	result.setDescription("end");
    }else
    	result.setDescription(Enum.valueOf(com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.class, activityId).toString());
    return result;
    
  }
  
  public void _start(final RCProject project, final Token token) {
    BeanFactory _beanFactory = this.getBeanFactory();
    ISystemUserService _bean = _beanFactory.<ISystemUserService>getBean(ISystemUserService.class);
    Party _currentUserAsParty = _bean.getCurrentUserAsParty();
    this.starter = ((RCEmployee) _currentUserAsParty);
    this.project = project;
    project.setStatus(RCProjectStatus.auditing);RCProjectProcessEvent rCProjectProcessEvent=RCProjectProcessEventImpl.create(project,getObjectFactory());
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          it.setBp(((BusinessProcess) RCInitializeProjectProcessImpl.this));
          it.setDescription("\u9879\u76EE\u7ACB\u9879\u7533\u8BF7\u4E2D");
          it.setStatus(RCProjectProcessEventStatus.executing);
          Date _day = DateUtil.today();
          it.setDate(_day);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(rCProjectProcessEvent, _function);
  }
  
  public void start(final RCProject project) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().start(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesManagerAudit.name());
    _start(project,token);
    getProcessManager().registTask(this, token, saleManager(),getSalesManagerAudit_task_message());
    systemExecute();
    String msg = getStart_log_message(project);
    getProcessManager().createActivity(this,"Create Process",msg);
    
    
  }
  
  public String getStart_log_message(final RCProject project) {
    return "\u53D1\u8D77\u9879\u76EE\u5BA1\u6279\u6D41\u7A0B";
  }
  
  public void SalesManagerAudit() {
  }
  
  public void _SalesManagerAudit_reject(final String comment, final Token token) {
    Token SalesModify_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesModify.name());
    getProcessManager().registTask(this, SalesModify_token,saler(),getSalesModify_task_message());
  }
  
  public void SalesManagerAudit_reject(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesManagerAudit.name());
    _SalesManagerAudit_reject(comment,token);
    systemExecute();
    String msg = getSalesManagerAudit_Reject_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批助理审批.总经理审批助理拒绝",msg);
    
    
  }
  
  public String getSalesManagerAudit_Reject_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406\u62D2\u7EDD,\u7406\u7531:" + _nullsafe);
    return _plus;
  }
  
  public void _SalesManagerAudit_agree(final String comment, final Token token) {
    Token CEOAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.CEOAudit.name());
    getProcessManager().registTask(this, CEOAudit_token,ceo(),getCEOAudit_task_message());
  }
  
  public void SalesManagerAudit_agree(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesManagerAudit.name());
    _SalesManagerAudit_agree(comment,token);
    systemExecute();
    String msg = getSalesManagerAudit_Agree_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批助理审批.总经理审批助理同意",msg);
    
    
  }
  
  public String getSalesManagerAudit_Agree_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406\u540C\u610F,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getSalesManagerAudit_task_message() {
    String _name = this.project.getName();
    String _plus = ("\u9879\u76EE\u540D\u79F0:" + _name);
    return _plus;
  }
  
  public void SalesModify() {
  }
  
  public void _SalesModify_modified(final String comment, final Token token) {
    Token SalesManagerAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesManagerAudit.name());
    getProcessManager().registTask(this, SalesManagerAudit_token,saleManager(),getSalesManagerAudit_task_message());
  }
  
  public void SalesModify_modified(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesModify.name());
    _SalesModify_modified(comment,token);
    systemExecute();
    String msg = getSalesModify_Modified_log_message(comment);
    getProcessManager().createActivity(this,"销售修改.销售修改项目信息",msg);
    
    
  }
  
  public String getSalesModify_Modified_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u9500\u552E\u4FEE\u6539\u9879\u76EE\u4FE1\u606F,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public void _SalesModify_cancel(final String comment, final Token token) {
    this.project.setStatus(RCProjectStatus.canceled);
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void SalesModify_cancel(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesModify.name());
    _SalesModify_cancel(comment,token);
    systemExecute();
    String msg = getSalesModify_Cancel_log_message(comment);
    getProcessManager().createActivity(this,"销售修改.销售取消项目申请",msg);
    
    
  }
  
  public String getSalesModify_Cancel_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u9500\u552E\u53D6\u6D88\u9879\u76EE\u7533\u8BF7,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getSalesModify_task_message() {
    String _name = this.project.getName();
    String _plus = ("\u9879\u76EE\u540D\u79F0:" + _name);
    return _plus;
  }
  
  public void CEOAudit() {
  }
  
  public void _CEOAudit_reject(final String comment, final Token token) {
    Token SalesManagerAudit_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.SalesManagerAudit.name());
    getProcessManager().registTask(this, SalesManagerAudit_token,saleManager(),getSalesManagerAudit_task_message());
  }
  
  public void CEOAudit_reject(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.CEOAudit.name());
    _CEOAudit_reject(comment,token);
    systemExecute();
    String msg = getCEOAudit_Reject_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批.总经理拒绝",msg);
    
    
  }
  
  public String getCEOAudit_Reject_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u62D2\u7EDD,\u7406\u7531:" + _nullsafe);
    return _plus;
  }
  
  public void _CEOAudit_agree(final String comment, final Token token) {
    this.project.setPassed(Boolean.valueOf(true));
    Date _day = DateUtil.today();
    this.project.setPassDate(_day);
    this.project.setStatus(RCProjectStatus.starting);
    final RCProjectProcessEvent event = this.getEvent();
    final Procedure1<RCProjectProcessEvent> _function = new Procedure1<RCProjectProcessEvent>() {
        public void apply(final RCProjectProcessEvent it) {
          it.setDescription("\u9879\u76EE\u7ACB\u9879\u5BA1\u6279\u901A\u8FC7");
          Date _day = DateUtil.today();
          it.setFinishDate(_day);
          it.setStatus(RCProjectProcessEventStatus.finished);
        }
      };
    ObjectExtensions.<RCProjectProcessEvent>operator_doubleArrow(event, _function);
    BeanFactory _beanFactory = this.getBeanFactory();
    RCParameterService _bean = _beanFactory.<RCParameterService>getBean(RCParameterService.class);
    String _nextProjectId = _bean.getNextProjectId();
    this.project.setCode(_nextProjectId);
    TaskService _taskService = this.getTaskService();
    final Procedure1<NormalTask> _function_1 = new Procedure1<NormalTask>() {
        public void apply(final NormalTask it) {
          it.setTaskTypeName("\u5F85\u5904\u7406\u9879\u76EE\u63D0\u9192");
          RCEmployee _businessMan = RCInitializeProjectProcessImpl.this.project.getBusinessMan();
          it.setOwner(_businessMan);
          String _code = RCInitializeProjectProcessImpl.this.project.getCode();
          String _plus = ("\u9879\u76EE\u5B8C\u6210\u5BA1\u6279\u6D41\u7A0B\uFF0C\u9879\u76EE\u7F16\u53F7\uFF1A" + _code);
          it.setDescription(_plus);
        }
      };
    _taskService.registerNormalTask(_function_1);
    RCProjectStat _stat = this.project.getStat();
    _stat.updateInfo();
    BeanFactory _beanFactory_1 = this.getBeanFactory();
    RCProjectEventsManager _bean_1 = _beanFactory_1.<RCProjectEventsManager>getBean(RCProjectEventsManager.class);
    _bean_1.fireProjectAuditPassed(this.project);
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void CEOAudit_agree(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.project.RCInitializeProjectProcessStatus.CEOAudit.name());
    _CEOAudit_agree(comment,token);
    systemExecute();
    String msg = getCEOAudit_Agree_log_message(comment);
    getProcessManager().createActivity(this,"总经理审批.总经理同意",msg);
    
    
  }
  
  public String getCEOAudit_Agree_log_message(final String comment) {
    String _nullsafe = StringExtensions.nullsafe(comment);
    String _plus = ("\u603B\u7ECF\u7406\u540C\u610F ,\u5907\u6CE8:" + _nullsafe);
    return _plus;
  }
  
  public String getCEOAudit_task_message() {
    String _name = this.project.getName();
    String _plus = ("\u9879\u76EE\u540D\u79F0:" + _name);
    return _plus;
  }
  
  @JoinColumn(name = "project_id")
  @ManyToOne(targetEntity = RCProjectImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCProject project;
  
  public RCProject getProject() {
    return this.project;
  }
  
  public RCInitializeProjectProcess setProject(final RCProject project) {
    this.project = project;
    return this;			
    
  }
  
  @JoinColumn(name = "starter_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee starter;
  
  public RCEmployee getStarter() {
    return this.starter;
  }
  
  public RCInitializeProjectProcess setStarter(final RCEmployee starter) {
    this.starter = starter;
    return this;			
    
  }
  
  public RCProjectProcessEvent getEvent() {
    IGenericQuery<RCProjectProcessEvent> query=getObjectFactory().createGenericQuery(RCProjectProcessEvent.class,"select e from com.shrct.businessprocess.project.impl.RCProjectProcessEventImpl as e   where ((e.bp) = (:this)) and  e.active = 1      ").setParameter("this",this);
    RCProjectProcessEvent _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public RCOrganizationService getOrganizationService() {
    BeanFactory _beanFactory = this.getBeanFactory();
    RCOrganizationService _bean = _beanFactory.<RCOrganizationService>getBean(RCOrganizationService.class);
    return _bean;
  }
  
  public TaskService getTaskService() {
    BeanFactory _beanFactory = this.getBeanFactory();
    TaskService _bean = _beanFactory.<TaskService>getBean(TaskService.class);
    return _bean;
  }
  
  public boolean _systemExecute() {
    boolean stateChanged = false;
    return stateChanged;
  }
  
  public static RCInitializeProjectProcess create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCInitializeProjectProcess rCInitializeProjectProcess = new com.shrct.businessprocess.project.impl.RCInitializeProjectProcessImpl();
    objectFactory.create(rCInitializeProjectProcess);
    return rCInitializeProjectProcess;			
    
  }
  
  public Party saler() {
    return this.starter;
  }
  
  public Party ceo() {
    RCEmployee _xblockexpression = null;
    {
      BeanFactory _beanFactory = this.getBeanFactory();
      RCCEORoleProvider _bean = _beanFactory.<RCCEORoleProvider>getBean(RCCEORoleProvider.class);
      final RCEmployee res = _bean.getParty();
      boolean _notEquals = (!Objects.equal(res, null));
      Validates.Assert(_notEquals, "\u672A\u8BBE\u7F6E\u603B\u7ECF\u7406");
      _xblockexpression = (res);
    }
    return _xblockexpression;
  }
  
  public Party saleManager() {
    RCEmployee _xblockexpression = null;
    {
      BeanFactory _beanFactory = this.getBeanFactory();
      RCSalesLeaderRoleProvider _bean = _beanFactory.<RCSalesLeaderRoleProvider>getBean(RCSalesLeaderRoleProvider.class);
      final RCEmployee res = _bean.getParty();
      boolean _notEquals = (!Objects.equal(res, null));
      Validates.Assert(_notEquals, "\u672A\u8BBE\u7F6E\u603B\u7ECF\u7406\u5BA1\u6279\u52A9\u7406");
      _xblockexpression = (res);
    }
    return _xblockexpression;
  }
  
  public RCInitializeProjectProcessProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCInitializeProjectProcessProxy proxy = new com.shrct.businessprocess.project.proxy.RCInitializeProjectProcessProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
